package com.silverhaze.diamond.client.ui.table.model;

import com.silverhaze.commons.db.datasource.DataSourceAction;
import com.silverhaze.commons.db.datasource.DataSourceListener;
import com.silverhaze.diamond.client.common.AuroraConstants;
import com.silverhaze.diamond.client.data.dao.DAOLocator;
import com.silverhaze.diamond.client.ui.UIStringConstants;
import com.silverhaze.diamond.client.ui.table.shells.ServiceShell;
import com.silverhaze.diamond.client.utils.db.ServiceDataSource;
import com.silverhaze.diamond.common.entity.Service;
import com.silverhaze.diamond.common.entity.SystemUser;

import java.util.List;

/**
 * Created by Ivan Babanin (14.07.2007 2:43:57)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
public class ServiceTableModel extends AbstractServiceTableModel<Service> {
    private List<Service> dataList;
    private static final int colCount = 7;

    public static final int OWNER_ROW = 0;
    public static final int DETAIL = 1;
    public static final int DATE_OF_PURCHASE = 3;
    public static final int STATUS = 6;

    public ServiceTableModel() {
        super();
        fillData(DAOLocator.getInstance().getServices());
        ServiceDataSource.getInstance().addDataSourceListener(new DataSourceListener<Service>() {
            public void valuesUpdated(Class<Service> clazz, List<Service> values, DataSourceAction action) {
                if (clazz.equals(Service.class)) {
                    refreshModelAll();
                }
            }
        });
    }

    public ServiceTableModel(List<Service> data) {
        super();
        fillData(data);
    }

    public void createFields() {
        setColCountSize(colCount);
        setColumnName(new String[]{
                UIStringConstants.DEVICE,
                "Серийный номер",
                "Гарантийный талон",
                UIStringConstants.OWNER,
                UIStringConstants.PURCHASE_DATE,
                "Дата приема",
                UIStringConstants.ADDED_BY});
    }

    public void fillData(List data) {
        int i = 0;

        this.dataList = data;
        setData(new Object[data.size()][colCount]);
//        setRowCount(data.size());

        for (Object o : data) {
            Service s = (Service) o;
            SystemUser u = s.getSystemUser();

            String serviceDate = s.getDateService() != null ? AuroraConstants.DATE_TIME_FORMAT_CLIENT.format(s.getDateService()) : "";

            getData()[i][0] = new ServiceShell<String>(s, s.getTechNames());
            getData()[i][1] = new ServiceShell<String>(s, s.getSerialNumber());
            getData()[i][2] = new ServiceShell<String>(s, s.getGuarantyId());
            getData()[i][3] = new ServiceShell<String>(s, s.getOwner().getExtendedName());
            getData()[i][4] = new ServiceShell<String>(s, AuroraConstants.DATE_TIME_FORMAT.format(s.getDateProduce()));
            getData()[i][5] = new ServiceShell<String>(s, serviceDate);
            getData()[i][6] = new ServiceShell<String>(s, u != null ? u.getLogin() : " - ");

            i++;
        }

        setRowCountSize(i);
        fireTableDataChanged();
    }

    public Service getObjectAtRow(int row) {
        assert row >= 0 && row < dataList.size();
        return ((ServiceShell) getValueAt(row, 0)).getObject();
    }

    public void refreshModelAll() {
        fillData(DAOLocator.getInstance().getServices());
        refreshModel();
    }
}